package com.ruoyi.standingbook.sorting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.sorting.domain.Sorting;
import com.ruoyi.standingbook.sorting.domain.SortingImportDetail;
import com.ruoyi.standingbook.sorting.domain.bo.SortingImportDetailBo;
import com.ruoyi.standingbook.sorting.domain.vo.SortingImportDetailVo;
import com.ruoyi.standingbook.sorting.mapper.SortingImportDetailMapper;
import com.ruoyi.standingbook.sorting.service.ISortingImportDetailService;
import com.ruoyi.standingbook.toilet.domain.ToiletImportDetail;
import com.ruoyi.standingbook.toilet.domain.vo.ToiletImportDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 垃圾分类督导导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-05-01
 */
@RequiredArgsConstructor
@Service
public class SortingImportDetailServiceImpl implements ISortingImportDetailService {

    private final SortingImportDetailMapper baseMapper;

    /**
     * 查询垃圾分类督导导入明细
     */
    @Override
    public SortingImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询垃圾分类督导导入明细列表
     */
    @Override
    public TableDataInfo<SortingImportDetailVo> queryPageList(SortingImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SortingImportDetail> lqw = buildQueryWrapper(bo);
        Page<SortingImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询垃圾分类督导导入明细列表
     */
    @Override
    public List<SortingImportDetailVo> queryList(SortingImportDetailBo bo) {
        LambdaQueryWrapper<SortingImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SortingImportDetail> buildQueryWrapper(SortingImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SortingImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), SortingImportDetail::getBatchNo, bo.getBatchNo());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), SortingImportDetail::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), SortingImportDetail::getCommunity, bo.getCommunity());
        lqw.like(StringUtils.isNotBlank(bo.getSuperintendentUserName()), SortingImportDetail::getSuperintendentUserName, bo.getSuperintendentUserName());
        lqw.like(StringUtils.isNotBlank(bo.getDeliveryName()), SortingImportDetail::getDeliveryName, bo.getDeliveryName());

        lqw.between(params.get("taskStartTime") != null && params.get("taskEndTime") != null,
            SortingImportDetail::getTaskStartTime ,params.get("taskStartTime"), params.get("taskEndTime"));
        lqw.between(params.get("taskStartTime") != null && params.get("taskEndTime") != null,
            SortingImportDetail::getTaskEndTime ,params.get("taskStartTime"), params.get("taskEndTime"));
        lqw.orderByDesc(SortingImportDetail::getCreateTime);
        lqw.orderByDesc(SortingImportDetail::getDate);
        return lqw;
    }

    /**
     * 新增垃圾分类督导导入明细
     */
    @Override
    public Boolean insertByBo(SortingImportDetailBo bo) {
        SortingImportDetail add = BeanUtil.toBean(bo, SortingImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改垃圾分类督导导入明细
     */
    @Override
    public Boolean updateByBo(SortingImportDetailBo bo) {
        SortingImportDetail update = BeanUtil.toBean(bo, SortingImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SortingImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除垃圾分类督导导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量插入
     */
    @Override
    public Boolean batchInsertVo(List<SortingImportDetailVo> voList) {
        List<SortingImportDetail> list= BeanCopyUtils.copyList(voList , SortingImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
